package com.ocom.payconfig.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfAccountType;
import com.ocom.common.entity.payconfig.EntityFkUserparamConfig;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.FkUserparamConfigVo;
import com.ocom.payconfig.service.FkUserparamConfigService;
import com.ocom.payconfig.service.XfAccountTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class FkUserparamConfigController {

    @Autowired
    private XfAccountTypeService xfAccountTypeService;

    @Autowired
    private FkUserparamConfigService fkUserparamConfigService;

    //获取风控
    @PostMapping(PayConfigUrl.Url_getUserFkConfig)
    public Result getUserFkConfig(Long comId, Integer merchId){
        if (comId == null || merchId==null) {
            return ResultUtil.error(ResultEnum.Err_203);
        }
        //获取风控
        List<EntityFkUserparamConfig> fkUserparamConfigList=fkUserparamConfigService.selectkUserparamConfigList(comId,merchId);
        List<EntityXfAccountType> xfAccountTypeList=xfAccountTypeService.selectXfAccountTypeList(comId);
        List<FkUserparamConfigVo> userparamConfigVoList=new ArrayList<>();
        for (EntityXfAccountType accountType : xfAccountTypeList) {
            FkUserparamConfigVo fkUserparamConfigVo=new FkUserparamConfigVo();
            fkUserparamConfigVo.setAccountTypeName(accountType.getAccTypeName());
            fkUserparamConfigVo.setAccountType(accountType.getAccTypeId());
            int state=0;
            for (EntityFkUserparamConfig entityFkUserparamConfig : fkUserparamConfigList) {
                if (accountType.getAccTypeId().compareTo(entityFkUserparamConfig.getUserType())==0){
                    fkUserparamConfigVo.setOrderAmount(entityFkUserparamConfig.getMaxamountOfflin());
                    fkUserparamConfigVo.setUserAmount(entityFkUserparamConfig.getOneamountOfflin());
                    fkUserparamConfigVo.setUserOrderNum(entityFkUserparamConfig.getOnetimesOffline());
                    state=1;

                    break;
                }
            }
            fkUserparamConfigVo.setState(state);
            userparamConfigVoList.add(fkUserparamConfigVo);
        }
        return ResultUtil.success(userparamConfigVoList);
    }

    //设置商户风控参数（账户类型）
    @PostMapping(PayConfigUrl.Url_setUserFkConfig)
    public Result setUserFkConfig(@RequestBody JSONObject jsonObject){
        if (jsonObject.getLong("comId") == null || jsonObject.getLong("merchId")==null || jsonObject.get("userPara")==null) {
            return ResultUtil.error(ResultEnum.Err_203);
        }
        List<FkUserparamConfigVo> list=JSON.parseArray(JSON.toJSONString(jsonObject.get("userPara")),FkUserparamConfigVo.class);
        for (FkUserparamConfigVo fkUserparamConfigVo : list) {
            if (fkUserparamConfigVo.getState()==0){
                fkUserparamConfigService.deleteFkUserparamConfig(jsonObject.getLong("comId"),jsonObject.getLong("merchId"),fkUserparamConfigVo.getAccountType());
            }else {
                EntityFkUserparamConfig entityFkUserparamConfig=new EntityFkUserparamConfig();
                entityFkUserparamConfig.setComId(jsonObject.getInteger("comId"));
                entityFkUserparamConfig.setMerchId(jsonObject.getInteger("merchId"));
                entityFkUserparamConfig.setUserType(fkUserparamConfigVo.getAccountType());
                entityFkUserparamConfig.setMaxamountOfflin(fkUserparamConfigVo.getOrderAmount());
                entityFkUserparamConfig.setOneamountOfflin(fkUserparamConfigVo.getUserAmount());
                entityFkUserparamConfig.setOnetimesOffline(fkUserparamConfigVo.getUserOrderNum());
                Date date=new Date();
                entityFkUserparamConfig.setUpdateUser(jsonObject.getString("user"));
                entityFkUserparamConfig.setUpdateTime(date);
                EntityFkUserparamConfig FkUserparamConfig=fkUserparamConfigService.selectEntityFkUserparamConfig(jsonObject.getLong("comId"),jsonObject.getLong("merchId"),fkUserparamConfigVo.getAccountType());
                if (FkUserparamConfig == null) {
                    entityFkUserparamConfig.setCreateUser(jsonObject.getString("user"));
                    entityFkUserparamConfig.setCreateTime(date);
                    fkUserparamConfigService.insetEntityFkUserparamConfig(jsonObject.getLong("comId"),entityFkUserparamConfig);
                }else {
                    fkUserparamConfigService.UpdateEntityFkUserparamConfig(jsonObject.getLong("comId"),entityFkUserparamConfig);
                }
            }
        }
        return ResultUtil.success();
    }
}
